package com.huike.business.service.Impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.dto.TbBusinessDto;
import com.huike.business.domain.dto.TbBusinessGainDto;
import com.huike.business.domain.dto.TbBusinessPoolDto;
import com.huike.business.domain.vo.TbBusinessVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.TbBusinessService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TbBusinessServiceImpl implements TbBusinessService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * 条件分页查询
     */
    @Override
    public List<TbBusinessVo> page(TbBusinessDto kdBusinessDto) {
        return tbBusinessMapper.page(kdBusinessDto);
    }

    /**
     * 添加商机
     *
     * @param kdBusiness
     */
    @Override
    public int add(TbBusiness kdBusiness) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        kdBusiness.setCreateTime(format.format(date));
        kdBusiness.setCreateBy(SecurityUtils.getUsername());
        kdBusiness.setStatus("1");
        return tbBusinessMapper.add(kdBusiness);
    }

    /**
     * 根据Id查询信息
     *
     * @param businessId
     */
    @Override
    public TbBusiness findById(Long businessId) {

        return tbBusinessMapper.findById(businessId);
    }

    /**
     * 修改商机

     */
    @Override
    public int update(TbBusiness tdBusiness) {


        return tbBusinessMapper.update(tdBusiness);
    }

    /**
     * 公海池条件分页查询
     *
     * @param kdBusinessPoolDto
     * @return
     */
    @Override
    public List<TbBusiness> poolPage(TbBusinessPoolDto kdBusinessPoolDto) {

        return tbBusinessMapper.poolPage(kdBusinessPoolDto);
    }

    /**
     * 退回公海
     *
     * @param id
     * @param reporting
     * @return
     */
    @Override
    public int returnSea(Long id, Long reporting) {
        //1.根据id查询商机信息
        TbBusiness business = tbBusinessMapper.findById(id);
        //2.更改商机状态,退回公海原因
        business.setStatus("2");
        business.setReporting(reporting);
        return tbBusinessMapper.updateStatusAndReporting(business);
    }

    /**
     * 公海池捞取商机
     *
     */
    @Override
    public int gain(TbBusinessGainDto kdBusinessGainDto) {
        return tbBusinessMapper.updateStatus(kdBusinessGainDto.getIds());
    }


}
